package codepulse.blogmoon.cn.system.dubbo;

import codepulse.blogmoon.cn.code.dto.CodeAccountDetailDTO;
import codepulse.blogmoon.cn.code.dubbo.CodeAccountDubboService;
import codepulse.blogmoon.cn.code.enums.SourceEnum;
import codepulse.blogmoon.cn.common.enums.StatusEnum;
import codepulse.blogmoon.cn.common.exception.BizException;
import codepulse.blogmoon.cn.common.utils.BaseConvert;
import codepulse.blogmoon.cn.system.dto.admin.AdminAcctInfoDTO;
import codepulse.blogmoon.cn.system.dto.SysAcctInfoDTO;
import codepulse.blogmoon.cn.system.dto.admin.RegisterDTO;
import codepulse.blogmoon.cn.system.entity.SysAccountInfo;
import codepulse.blogmoon.cn.system.entity.SysAdminAcctInfo;
import codepulse.blogmoon.cn.system.entity.SysAdminAcctRoleRel;
import codepulse.blogmoon.cn.system.entity.SysBizAccount;
import codepulse.blogmoon.cn.system.service.SysAccountInfoService;
import codepulse.blogmoon.cn.system.service.SysAdminAcctInfoService;
import codepulse.blogmoon.cn.system.service.SysBizAccountService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@DubboService
public class AcctInfoDubboServiceImpl implements AcctInfoDubboService {

    @Resource
    private SysAccountInfoService sysAccountInfoService;

    @Resource
    private SysAdminAcctInfoService sysAdminAcctInfoService;

    @Resource
    private SysBizAccountService sysBizAccountService;

    @DubboReference(timeout = 5000)
    private CodeAccountDubboService codeAccountDubboService;

    @Override
    public SysAcctInfoDTO getAcctInfo(Long id) {
        SysAccountInfo sysAccountInfo = sysAccountInfoService.getOne(Wrappers.<SysAccountInfo>lambdaQuery()
                .eq(SysAccountInfo::getBizAcctId, id));
        SysBizAccount sysBizAccount = sysBizAccountService.getById(id);
        SysAcctInfoDTO sysAcctInfoDTO = BaseConvert.convert(sysAccountInfo, SysAcctInfoDTO.class);
        sysAcctInfoDTO.setEmail(sysBizAccount.getEmail());
        sysAcctInfoDTO.setMobile(sysBizAccount.getMobile());
        sysAcctInfoDTO.setPassword(sysBizAccount.getPassword());
        return sysAcctInfoDTO;
    }

    @Override
    public AdminAcctInfoDTO getAdminAcctInfo(Long id) {
        SysAdminAcctInfo sysAdminAcctInfo = sysAdminAcctInfoService.getOne(Wrappers.<SysAdminAcctInfo>lambdaQuery()
                .eq(SysAdminAcctInfo::getBizAcctId, id));

        if (Objects.equals(StatusEnum.DISABLE.getCode(), sysAdminAcctInfo.getStatus())) {
            throw new BizException("用户被禁用");
        }
        return BaseConvert.convert(sysAdminAcctInfo, AdminAcctInfoDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(RegisterDTO dto) {
        SysBizAccount sysBizAccount = sysBizAccountService.getOne(Wrappers.<SysBizAccount>lambdaQuery()
                .eq(SysBizAccount::getMobile, dto.getMobile()));
        if (Objects.isNull(sysBizAccount)) {
            if (StringUtils.isBlank(dto.getPassword())) {
                dto.setPassword(dto.getMobile().substring(dto.getMobile().length() - 6));
            }
            sysBizAccount = new SysBizAccount();
            sysBizAccount.setCreateField();
            sysBizAccount.setEmail(dto.getEmail());
            sysBizAccount.setPassword(new BCryptPasswordEncoder().encode(dto.getPassword()));
            sysBizAccount.setMobile(dto.getMobile());
            sysBizAccountService.save(sysBizAccount);
        }
        SysAccountInfo baseAdminInfo = sysAccountInfoService.getOne(Wrappers.<SysAccountInfo>lambdaQuery()
                .eq(SysAccountInfo::getBizAcctId, sysBizAccount.getId()));
        if (Objects.nonNull(baseAdminInfo)) {
            throw new BizException("当前手机号已存在对应用户");
        }
        SysAccountInfo sysAdminAcctInfo = BaseConvert.convert(dto, SysAccountInfo.class);
        sysAdminAcctInfo.setBizAcctId(sysBizAccount.getId());
        sysAccountInfoService.save(sysAdminAcctInfo);


        CodeAccountDetailDTO accountDetailDTO = new CodeAccountDetailDTO();
        accountDetailDTO.setSource(SourceEnum.GITEA.getType());
        accountDetailDTO.setMobile(dto.getMobile());
        accountDetailDTO.setRealName(dto.getRealName());
        accountDetailDTO.setBizAcctId(sysBizAccount.getId());
        accountDetailDTO.setEmail(dto.getEmail());
        accountDetailDTO.setPassword(dto.getPassword());
        codeAccountDubboService.createCodeAccount(accountDetailDTO);

    }
}
